#include "selfBulletManager.h"
#include "stdBulletLib.h"
#include "emenyManager.h"
#include "bossManager.h"

selfBulletManager* selfBulletManager::_instance = 0;

selfBulletManager* selfBulletManager::Instance()
{
	if (_instance == NULL)
	{
		_instance = new selfBulletManager();
	}
	return _instance;
}

selfBulletManager::selfBulletManager()
{
	sumNum = 0;
	for (int i = 0; i < 1000; i++)
	{
		freeMem.push_back(new selfBullet());
	}
	//manager.resize(100);
}

void selfBulletManager::draw()
{
	for (list<selfBullet*>::iterator itor = manager.begin();
		itor != manager.end(); itor++)
	{
		(*itor)->draw();
	}
}


/*
void selfBulletManager::add(selfBullet* b)
{
	manager.push_front(b);
}*/

void selfBulletManager::add(stdSelfBullet std, Point p, int power, int kind)
{
	list<selfBullet*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(std, p, power, kind);
	manager.splice(manager.begin(), freeMem, itor);
}

void selfBulletManager::add(stdSelfBullet std, Point p, int power, int kind, int angle)
{
	list<selfBullet*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(std, p, power, kind, angle);
	manager.splice(manager.begin(), freeMem, itor);
}

void selfBulletManager::updata()
{
	cout << freeMem.size() << " " << manager.size() << endl;
	selfBullet b;
	emenyManager* eManager = 0;
	eManager = eManager->Instance();
	bossManager* bManager = 0;
	bManager = bManager->Instance();
	if (!eManager->checkEmpty())
	{
		b.setPoint(&eManager->getPoint());	
	}else if (!bManager->checkEmpty())
	{
		b.setPoint(&bManager->getPoint());
	}else
	{
		b.setPoint(NULL);
	}
	for (list<selfBullet*>::iterator itor = manager.begin();
		itor != manager.end();)
	{
		if ((*itor)->checkRemove())
		{
			list<selfBullet*>::iterator itor1 = itor;
			itor1++;
			freeMem.splice(freeMem.begin(), manager, itor);
			itor = itor1;
		}else
		{
			(*itor)->updata();
			itor++;
		}
	}
}

void selfBulletManager::clear()
{
	freeMem.splice(freeMem.begin(), manager);
}